package com.ezjie.ielts.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;

import android.content.Context;
import android.os.Environment;

import com.ezjie.ielts.R;

/**
 * DownloadManager下载的查询 等方法
 * 
 * @author renyangyang
 *
 */
public class DownloadUtil {
	public static final String DATABASE_PATH = android.os.Environment.getExternalStorageDirectory().getAbsolutePath()
			+ "/easyjie_ielts";
	public static final String DATABASE_FILENAME = "easy_dic.db";// 核心词数据库

	public static String WORD_CACHE_PATH;// 单词版本 数据库缓存的路径
	public static final String WORD_DATABASE_FILENAME = "easy_word.db";// 单词数据库

	private static DownloadUtil downloadUtil = null;

	private DownloadUtil() {

	}

	public static DownloadUtil getInstance() {
		if (downloadUtil == null) {
			downloadUtil = new DownloadUtil();
		}

		return downloadUtil;
	}

	/**
	 * 把raw中db文件拷贝到sd卡中
	 */
	public boolean copyDB2SD(Context context) {
		boolean copySuccess = false;
		try {
			// 获得db文件的绝对路径
			String databaseFilename = DownloadUtil.DATABASE_PATH + "/" + DownloadUtil.DATABASE_FILENAME;
			File dir = new File(DownloadUtil.DATABASE_PATH);
			if (!dir.exists()) {
				dir.mkdir();
			}
			if (!(new File(databaseFilename)).exists()) {
				InputStream is = context.getResources().openRawResource(R.raw.easy_dic);
				FileOutputStream fos = new FileOutputStream(databaseFilename);
				byte[] buffer = new byte[8192];
				int count = 0;
				// 开始复制dictionary.db文件
				while ((count = is.read(buffer)) > 0) {
					fos.write(buffer, 0, count);
				}

				fos.close();
				is.close();
			}
			copySuccess = true;
		} catch (Exception e) {
			LogUtils.d("初始化字典数据失败");
			LogUtils.exception(e);
			copySuccess = false;
		}
		return copySuccess;
	}

	public void copyWordDB2SD(final Context context) {
		boolean is_word_db_exist = PreferencesUtil.getBoolean(context, WordHomeDataUtil.WORD_IS_EXIST, false);
		if (!is_word_db_exist) {
			new Thread() {
				@Override
				public void run() {
					System.out.println("开始时间：" + DateUtil.getCurrentTime());
					try {
						// 获得db文件的绝对路径
						String databaseFilename = DownloadUtil.WORD_CACHE_PATH + "/"
								+ DownloadUtil.WORD_DATABASE_FILENAME;
						System.out.println("*********databaseFilename********" + databaseFilename);
						File dir = new File(DownloadUtil.WORD_CACHE_PATH);
						if (!dir.exists()) {
							dir.mkdir();
						}

						File file = new File(databaseFilename);
						if (file.exists()) {
							// 如果存在 就删除掉
							boolean delete = file.delete();
							if (delete) {
								System.out.println("存在，就删除了。");
							}
						}

						InputStream is = context.getResources().openRawResource(R.raw.easy_word);
						FileOutputStream fos = new FileOutputStream(databaseFilename);
						byte[] buffer = new byte[8192];
						int count = 0;
						// 开始复制dictionary.db文件
						while ((count = is.read(buffer)) > 0) {
							fos.write(buffer, 0, count);
						}

						fos.close();
						is.close();

						PreferencesUtil.putBoolean(context, WordHomeDataUtil.WORD_IS_EXIST, true);
					} catch (Exception e) {
						LogUtils.d("初始化单词数据库失败");
						LogUtils.exception(e);

					}
					System.out.println("结束时间：" + DateUtil.getCurrentTime());
				}
			}.start();
		}
	}

	/**
	 * 获得 缓存路径 如果有sdcard就存放在 sdcard/android/data/包名/file下面 如果没有scard就存放在
	 * data/data/包名/file下面
	 * 
	 * @param context
	 * @return
	 */
	public static void initCacheDir(Context context) {
		if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
				|| !Environment.isExternalStorageRemovable()) {
			File file = new File(DATABASE_PATH);

			if (!file.exists()) {
				file.mkdir();
			}

			WORD_CACHE_PATH = file.getPath();

		} else {
			File filesDir = context.getFilesDir();
			if (filesDir != null) {
				WORD_CACHE_PATH = filesDir.getPath();
			} else {
				filesDir = Environment.getDataDirectory();
				WORD_CACHE_PATH = filesDir.getPath();
			}
		}

	}
}
